package com.buss.customer.dao.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.buss.core.persistence.BaseDao;
import com.buss.core.persistence.ResultList;
import com.buss.core.utils.StringUtil;
import com.buss.customer.dao.CustomerContactsDao;
import com.buss.customer.model.CustomerContacts;

@Repository
public class CustomerContactsDaoImpl implements CustomerContactsDao {

	@Autowired
	private BaseDao<CustomerContacts> baseDao;
	
	@Override
	public CustomerContacts saveContacts(CustomerContacts contacts) {
		return baseDao.saveOrEdit(contacts);
	}

	@Override
	public void editContacts(CustomerContacts contacts) {
		baseDao.edit(contacts);
	}

	@Override
	public void removeContacts(Long[] ids) {
		String sql = "delete from CustomerContacts contacts where contacts.id in (:ids)";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("ids", ids);
		baseDao.excuteHsql(sql, params);
	}

	@Override
	public CustomerContacts loadContacts(Long id) {		
		return baseDao.get(id, CustomerContacts.class);
	}

	@Override
	public ResultList<CustomerContacts> queryContacts(Long customerId,
			String name) {
		String sql = "select contacts from CustomerContacts contacts where contacts.customerId = :customerId ";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("customerId", customerId);
		if(StringUtil.isNotEmpty(name)){
			sql += " and contacts.name like :name ";
			params.put("name", "%" + name + "%");
		}
		return baseDao.queryHsql(sql, params, null);
	}

}
